package org.hongbo.springbootebsapi.service.user;


import org.hongbo.springbootebsapi.entity.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Map;

/**
 *
 */
public class CustomUserServiceImpl implements UserDetailsService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        String userSql = "SELECT fu.user_id, fu.user_name\n" +
                "  FROM fnd_user fu\n" +
                " WHERE fu.user_name = ?";

        Map<String, Object> userMap = jdbcTemplate.queryForMap(userSql,new Object[]{ username });
        String userId = String.valueOf(userMap.get("USER_ID"));
        String userName = (String)userMap.get("USER_NAME");

        AuthUser user = new AuthUser();
        user.setUserId(userId);
        user.setUsername(userName);

        return user;
    }
}
